#include "do_sqlite.h"
#include "head.h"
#include <stdio.h>
#include <string.h>
#define _DUG_ 0
#define N 1024

#define table_name1 "person"
#define table_name2 "login"

int db_init(char *db_name)
{
	sqlite3 *db;
	int ret;
	char *errmsg;
	if( _creat_db(db_name,&db,&errmsg)!=0){
		return -1;
	}
	if(_open_db(db_name,&db,&errmsg)!=0){
		return -1;
	}
	if(_creat_table(table_name1,db,&errmsg)!=0){
		_close_db(db);
		return -1;
	}
	if(_creat_table(table_name2,db,&errmsg)!=0){
		_close_db(db);
		return -1;
	}
	sqlite3_exec(db,"insert into login values(?,'admin','12345',12);",NULL,NULL,NULL);
	_close_db(db);
	return 0;
}

int _creat_db(char db_name[20], sqlite3 **db, char **errmsg)
{
#if _DUG_
	printf("name:%s\n",db_name);
#endif

	return do_creat_db(db,db_name,errmsg);
}

int _open_db(char db_name[20], sqlite3 **db, char **errmsg)
{
#if _DUG_
	printf("name:%s\n",db_name);
#endif

	return do_open_db(db,db_name,errmsg);
}

int _creat_table(char name[],sqlite3 *db, char **errmsg)
{
	char sql[N] = {};
	
	if(strncmp(name,"person",6) == 0){
		printf("oooooooo----\n");
		sprintf(sql,"create table if not exists person(num integer PRIMARY KEY AUTOINCREMENT,name char, age int, telphone char , address char);");
	}else if(strncmp(name,"login",5) == 0){
		sprintf(sql,"create table if not exists %s(num integer PRIMARY KEY AUTOINCREMENT,name char, password char, authority int)",name);
	}
	else{
		return -1;
	}
	return do_creat_table(db,sql,errmsg);
}

int _delete(MSG_T *msg,sqlite3 *db, char **errmsg)
{
	char sql[N] = {};
	char *name = msg->user_info.name;
	
	sprintf(sql,"delete from login  where name = '%s';",name);

	do_delete_user(db,sql,errmsg);	

	sprintf(sql,"delete from person  where name = '%s';",name);

	return do_delete_user(db,sql,errmsg);	
}

int _add(MSG_T *msg,sqlite3 *db, char **errmsg)
{
	char sql[N] = {};
	int  num;
	int ret;
	char *name = msg->user_info.name;
	char *password = msg->user_info.password;
	int  age = msg->user_info.age;
	char *telphone = msg->user_info.telphone;
	char *address = msg->user_info.address;
	int  authority = USER;

//sprintf(sql,"create table if not exists person(num int PRIMARY KEY,name char, age int, telphone char , address char);");
	sprintf(sql,"insert into person values(?,'%s',%d,'%s','%s');",\
			name,age,telphone,address);
	ret = do_add_info(db,sql,errmsg);
#if _DUG_
	printf("_add ret1:%d\n",ret);
	if(ret !=0){
		printf("_add errmsg:%s\n",*errmsg);
	}
#endif
	sprintf(sql,"insert into login values(?,'%s','%s',%d);",\
			name,password,authority);
	ret = do_add_info(db,sql,errmsg);
#if _DUG_
	printf("_add ret2:%d\n",ret);
	if(ret !=0){
		printf("_add errmsg:%s\n",*errmsg);
	}
#endif
	return ret;
}

int _modify(MSG_T *msg,sqlite3 *db, char **errmsg)
{
	char sql[N]={};
	char sql1[N]={};
	int ret;

	sprintf(sql,"update person set age =%d, telphone='%s', address = '%s' where name = '%s';",msg->user_info.age,\
			msg->user_info.telphone,msg->user_info.address,msg->user_info.name);
	ret = do_update_user(db,sql,errmsg);

	printf("-------------password:%s  name:%s\n",msg->user_info.password,msg->user_info.name);
#if _DUG_
	printf("update1 sql:%s\n",sql);
	printf("-------------password:%s  name:%s\n",msg->user_info.password,msg->user_info.name);
#endif

	sprintf(sql1,"update login set password = '%s' where name = '%s';",msg->user_info.password,msg->user_info.name);
	ret = do_update_user(db,sql1,errmsg);
#if _DUG_
	printf("update2 sql:%s\n",sql);
#endif

	return ret;
}

int _register(MSG_T *msg, sqlite3 *db, char **errmsg)
{
	char sql[N] = {};
	int  num;
	int ret;
	char *name = msg->user_info.name;
	char *password = msg->user_info.password;
	int  age = msg->user_info.age;
	char *telphone = msg->user_info.telphone;
	char *address = msg->user_info.address;
	int  authority = USER;

sprintf(sql,"insert into person values(?,'%s',?,%d,'%s','%s');",\
			name,age,telphone,address);
	ret = do_register_user(db,sql,errmsg);
#if _DUG_
	printf("_add ret1:%d\n",ret);
	if(ret !=0){
		printf("_add errmsg:%s\n",*errmsg);
	}
#endif
	sprintf(sql,"insert into login values(?,'%s','%s',%d);",\
			name,password,authority);
	ret = do_register_user(db,sql,errmsg);
#if _DUG_
	printf("_add ret2:%d\n",ret);
	if(ret !=0){
		printf("_add errmsg:%s\n",*errmsg);
	}
#endif
	return ret;
}

int _que_person(MSG_T *msg, sqlite3 *db, char **errmsg)
{
	
	char sql[N] = {};
	sprintf(sql,"select * from person where name = '%s';",msg->user_info.name);
#if _DUG_
	printf("query sql:%s\n",sql);
#endif

	return do_query_person(db,sql,msg,errmsg);

}

int _que_login(MSG_T *msg,sqlite3 *db, char **errmsg)
{
	char sql[N] = {};
	sprintf(sql,"select * from login where name = '%s' and password = '%s' and authority = %d;",msg->user_info.name,\
			msg->user_info.password,msg->usertype);
#if _DUG_
	printf("query login  sql:%s\n",sql);
#endif
	return do_query_login(db,sql,msg,errmsg);
}


int _que_root_person(MSG_T *msg, sqlite3 *db, char **errmsg,int i)
{
	char sql[N]={};

	sprintf(sql,"select * from person limit 1 offset %d;",i-1);
#if _DUG_
	printf("query login  sql:%s\n",sql);
#endif

	return do_query_person(db,sql,msg,errmsg);
}

int _close_db(sqlite3 *db)
{
	return do_close_db(db);
}
